WebElement Methods in Selenium
The WebElement interface provides a variety of methods to interact with elements on a web page. Here are some commonly used methods:
1. Finding Elements
findElement(By by):
Locates a single child element of the current element using a given mechanism.findElements(By by):
Locates all child elements of the current element that match the given mechanism.
2. Element Interactions
click():
Clicks on the element.sendKeys(CharSequence... keysToSend):
Types into the element.clear():
Clears the content of the element if it is a text entry element.
3. Getting Element Attributes and Properties
getAttribute(String name):
Gets the value of the specified attribute of the element.getCssValue(String propertyName):
Retrieves the value of a CSS property.getText():
Retrieves the visible (i.e., not hidden by CSS) inner text of this element.getTagName():
Gets the tag name of this element.isDisplayed():
Determines whether the element is visible.isEnabled():
Checks if the element is enabled.isSelected():
Checks if the element is selected (for checkboxes, radio buttons, etc.).
4. Element Coordinates
getLocation():
Gets the location of the top-left corner of the element relative to the top-left corner of the page.getSize():
Gets the size of the element.
5. Managing Web Elements
submit():
Submits a form element.
These methods allow you to interact with web elements effectively during test automation. Depending on your use case, you may use a combination of these methods to interact with and validate web elements on the page.